<?php
namespace Admin\Controller;
use Think\Controller;

class ArticleController extends CommonController{
    
    public function lst(){
        $article = D('ArticleView');
        $count      = $article->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $article->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
    
    public function add(){
        $article = D('article');
        if (IS_POST){
            $data['title'] = I('title');
            $data['cateid'] = I('cateid');
            $data['desc'] = I('desc');
            $data['content'] =I('content');
            $data['time'] = time();
            if ($_FILES['pic']['tmp_name']!=''){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './'; // 设置附件上传根目录
                $upload->savePath  =     './Public/Uploads/'; // 设置附件上传（子）目录
                // 上传文件
                $info   =   $upload->uploadOne($_FILES['pic']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功
                    $data['pic'] = $info['savepath'].$info['savename'];
                }
            }else{
                $data['pic'] = "";
            }
            if ($article->create($data)){
                if ($article->add()){
                    $this->success('文章添加成功',U('lst'));
                }else{
                    $this->error('文章添加失败');
                }
            }else {
                $this->error($article->getError());
            }
            return ;
        }
        $cate = D('cate')->select();
        $this->assign('cate',$cate);
        $this->display();
    }
    
    public function edit(){
        $article = D('Article');
        $ar =$article->find(I('id'));
        $imgurl=$ar['pic'];
        if (IS_POST){
            $data['id']= I('id');
            $data['title'] = I('title');
            $data['cateid'] = I('cateid');
            $data['desc'] = I('desc');
            $data['content'] =I('content');
            $data['time'] = time();
            if ($_FILES['pic']['tmp_name']!=''){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './'; // 设置附件上传根目录
                $upload->savePath  =     './Public/Uploads/'; // 设置附件上传（子）目录
                // 上传文件
                $info   =   $upload->uploadOne($_FILES['pic']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    // 上传成功
                    $data['pic'] = $info['savepath'].$info['savename'];
                    if ($data['pic']!=''){
                        unlink($imgurl);
                    }
                }
            }else{
                $data['pic'] = I('pic');
            }
            if ($article->create($data)){
                $save = $article->save();
                if ($save!==false){
                    $this->success('文章修改成功',U('lst'));
                }else{
                    $this->error('文章修改失败');
                }
            }else {
                $this->error($article->getError());
            }
            return ;
        }
        $cate = D('cate')->select();
        $this->assign('cate',$cate);        
        $this->assign('ar',$ar);
        $this->display();
    }
    
    public function del(){
        $article = D('article');
        if($article->delete(I('id'))){
            $this->success('删除成功',U('lst'));
        }else{
            $this->error('删除失败');
        }
    }    
    
    public function fid(){
        $article =D('ArticleView');
        $id = I('cateid');
        $list      = $article->where('cateid='.$id)->select();
        $count      = $article->where('cateid='.$id)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $article->where('cateid='.$id)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
}